package controllers

import (
	"github.com/go-errors/errors"
	"github.com/myaccount/my-app/app/error"
	"github.com/myaccount/my-app/app/models"
	"github.com/myaccount/my-app/app/utils"
	"github.com/revel/revel"
	"gopkg.in/mgo.v2/bson"
)

type Card struct {
	Controller
}

func (c Card) ShareCard() revel.Result {
	var card models.Card
	var userId models.UserId
	if err := utils.ParseBodyAndValidate(c.body, &struct {
		Card          models.Card `json:"card" valid:"Required"`
		models.UserId `,inline`
	}{card, userId}); err != nil {
		return c.RenderError(error.ParseError, errors.Wrap(err, 2))
	}

	// get user info by qid
	if err := models.GetUser(userId.Qid, &card.Author); err != nil {
		return c.RenderError(error.InternalError, errors.Wrap(err, 2))
	}

	//TODO: link content
	type unlinked []map[string]string
	var notFound unlinked
	//	if _,err := func(contents []models.ContentProperty)(unlinked, error) {
	//		return nil,nil
	//	}(nil); err != nil {
	//		return c.RenderError(InternalError, errors.Wrap(err, 2))
	//	}
	// create card
	if err := card.Add(card); err != nil {
		return c.RenderError(error.InternalError, errors.Wrap(err, 2))
	}

	return c.RenderResult(struct {
		ErrCode  int      `json:"errCode"`
		ErrMsg   string   `json:"errMsg"`
		ToUpload unlinked `json:"toUpload"`
	}{ErrCode: 0, ErrMsg: "", ToUpload: notFound})
}

func (c Card) PraiseCard() revel.Result {
	type Request struct {
		CardId        bson.ObjectId `json:"cardId" valid:"Required"`
		models.UserId `,inline`
	}
	var req Request
	if err := utils.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderError(error.ParseError, errors.Wrap(err, 2))
	}
	var card models.Card
	if err := card.GetId(req.CardId, &card); err != nil {
		return c.RenderError(error.InternalError, errors.Wrap(err, 2))
	}
	// praise
	praiseCard := &models.PraisedCard{}
	if err := praiseCard.Add(req.Qid, card); err != nil {
		return c.RenderError(error.InternalError, errors.Wrap(err, 2))
	}

	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) UndopraiseCard() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) GetCardList() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) GetCard() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) GetContentList() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) ShieldCard() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) CardsOfSb() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}

func (c Card) CardsOfSbPraised() revel.Result {
	return c.RenderResult(struct {
		ErrCode int    `json:"errCode"`
		ErrMsg  string `json:"errMsg"`
	}{ErrCode: 0, ErrMsg: ""})
}
